﻿using System;
using Jing.TurbochargedScrollList;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ListItem : MonoBehaviour
{
    public Text textLabel;

    public TextMeshProUGUI redPoint;

    private ulong lastId;

    public Button btn;

    private void Awake()
    {
        btn.onClick.AddListener(OnBtnClick);
    }

    private void OnBtnClick()
    {
        MailMgr.Instance.SetReaded(lastId);
    }

    public void Refresh(float w, float h, string content,ulong id)
    {
        var item = gameObject.GetComponent<ScrollListItem>();
        
        textLabel.text = content;

        if (lastId != id && lastId>0)
        {
            RedPointMgr.Instance.GetNode(ERedPoint.Mail).GetOrAddRedPointNode(lastId)
                .ClearOnRedPointStateChangeListener();
        }
        RedPointMgr.Instance.GetNode(ERedPoint.Mail).GetOrAddRedPointNode(id)
            .AddOnRedPointStateChangeListener(node =>
            {
                redPoint.transform.parent.gameObject.SetActive(node.RedCount>0);
                redPoint.text = node.RedCount.ToString();
            });
        lastId = id;
        
        var rt = GetComponent<RectTransform>();
        rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, w);
        rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, h);
    }
}
